import { constantRoutes, asyncRoutes } from '@/router'

const state = {
  // 默认的路由数组只有静态的路由规则
  routes: constantRoutes
}

const mutations = {
  // 每一个用户登录进来, 都应该是基于静态路由去新增一些动态的路由
  setRoutes(state, otherRoutes) {
    // 这一步很漂亮
    state.routes = [...constantRoutes, ...otherRoutes]
  }
}

const actions = {
  // 1. 根据menus数组去筛选路由
  filterRoutes(context, menus) {
    // 获取到了该用户能够访问的路由对象
    const otherRoutes = asyncRoutes.filter(item => {
      return menus.includes(item.children[0].name)
    })

    context.commit('setRoutes', otherRoutes)

    // console.log(otherRoutes, '789032323232')
    return otherRoutes
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
